<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/11/6
 * Time: 8:11 PM
 */

namespace App\Library\User;


use App\Models\Order;
use App\Models\User;

class CommissionManager
{
    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * 获得消费积分
     * @param Order $order
     */
    public static function addConsumptionCommission(Order $order)
    {
        $buyer = $order->buyer;
        $relation = $buyer->relation;
        if ($relation)
        {
            $upuser = $relation->upuser;
            if ($upuser)
            {
                $group = $upuser->group;
                if ($group)
                {
                    //按会员等级获得佣金
                    $amount = $order->total_fee * $group->commission_rate/100;
                    $wallet = $upuser->wallet()->firstOrCreate([]);
                    $wallet->commission+= $amount;
                    $wallet->save();

                    $upuser->commissionRecords()->create([
                        'order_id'=>$order->order_id,
                        'amount'=>$amount,
                        'trade_type'=>1,
                        'subject'=>'消费返佣',
                        'body'=>substring($buyer->username, 5).'--消费返佣',
                        'type'=>1,
                        'buyer_uid'=>$buyer->uid,
                        'buyer_name'=>$buyer->username
                    ]);
                    //增加节点佣金
                    AchievementManager::addNodeCommission($upuser, $amount);
                }

                $relation = $upuser->relation;
                if ($relation)
                {
                    $superuser = $relation->upuser;
                    if ($superuser)
                    {
                        $amount = $order->total_fee * 0.05;
                        $wallet = $superuser->wallet()->firstOrCreate([]);
                        $wallet->commission+= $amount;
                        $wallet->save();

                        $superuser->commissionRecords()->create([
                            'order_id'=>$order->order_id,
                            'amount'=>$amount,
                            'trade_type'=>1,
                            'subject'=>'消费返佣',
                            'body'=>substring($buyer->username, 5).'--'.substring($upuser->username, 5).'--消费返佣',
                            'type'=>2,
                            'buyer_uid'=>$buyer->uid,
                            'buyer_name'=>$buyer->username
                        ]);
                        AchievementManager::addNodeCommission($superuser, $amount);
                    }
                }
            }
        }
    }

    public static function takeOffCommission(User $user, $amount, $body)
    {
        $manager = new static($user);
        $wallet = $manager->user->wallet()->firstOrCreate([]);
        if ($wallet->commission >= $amount)
        {
            $wallet->commission-= $amount;
            $wallet->save();
            $user->commissionRecords()->create([
                'amount'=>$amount,
                'trade_type'=>0,
                'body'=>$body,
                'subject'=>'佣金转让扣除',
                'type'=>0
            ]);

            return true;
        }

        return false;
    }
}
